# -*- coding: utf-8 -*-
import sys
sys.path.append('../..')
from common.config import *
from db.mysql import MysqlClient
from itertools import product
import requests
from common.helper import Helper
from urllib.parse import  urlencode
class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "中信保诚「亲恩保」恶性肿瘤疾病保险A款"
    def genProductInfo(self):
        plan = {
            "planA":{
                #年龄段
                "year":[str(i) for i in range(50,71)],
                #性别
                "gender":["M","F"],
                #保额
                "money":[str(i) + "0000" for i in range(5,25,5)],
                #交费间隔
                "interval":["5","10"],
                #交费类型
                "payType":["年交"],
            },
            "planB":{
                # 年龄段
                "year": [str(i) for i in range(71, 76)],
                # 性别
                "gender": ["M", "F"],
                # 保额
                "money": [str(i) + "0000" for i in range(5, 25, 5)],
                # 交费间隔
                "interval": ["5"],
                # 交费类型
                "payType": ["年交"],
            }
        }
        for k in plan:
            for v in product( plan[k]["year"],plan[k]["gender"],plan[k]["money"],plan[k]["interval"],plan[k]["payType"]):
                if v[3] == "5":
                    expire = "5"
                else:
                    expire = "10"
                data = Helper.formatProductInfo( plan[k], v )
                data["expire"] = expire
                self.mysql.cursor.execute(domain[self.name]["insert_sql"].format( **data ) )
                self.mysql._conn.commit()
                print(domain[self.name]["insert_sql"].format( **data )  )
    #执行爬虫
    def requestJson(self,dt=None,mysql=None):
        params = '''
        dutyCode: 555
        planCode: QEB1M001
        riskCode: 555
        productCode: QEB1
        insuredTime: 5Y
        payterm: 5
        age: 75
        paytype: Y
        sex: F
        amount: 20WY
        quoteno: S1586739597334545223
        '''
        data = Helper.perfectJson( params )
        data["insuredTime"] = dt["interval"] + "Y"
        data["payterm"] = dt["expire"]
        data["age"] = dt["year"]
        data["sex"] = dt["gender"]
        data["amount"] = str( int( int( dt["money"])/10000)) + "WY"
        payload = urlencode( data ).replace("+","")
        r = requests.post( url = domain[self.name]["url"],headers=domain[self.name]["header"],data=payload)
        if r.status_code == 200 and "premium" in r.text:
            price = Helper.decimalParse( r.json()["quoteResult"]["premium"])
            data  = {
                "name":self.name,
                "id":dt["id"],
                "price":price
            }
            print( data )
            mysql.updatePrice( data )
        else:
            data  = {
                "name":self.name,
                "id":dt["id"],
                "state":3
            }
            mysql.updateTask( data )
            print("\n/*更新产品价格失败:%d*/"%dt["id"])

